{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from IPython.core.interactiveshell import InteractiveShell\n",
    "InteractiveShell.ast_node_interactivity = 'all' # default is ‘last_expr’\n",
    "\n",
    "%load_ext autoreload\n",
    "%autoreload 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "sys.path.append('/path/to/CameraTraps')  # CameraTraps repository base dir"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "import copy\n",
    "import os\n",
    "from datetime import datetime\n",
    "import json\n",
    "\n",
    "from azure.cosmos.cosmos_client import CosmosClient\n",
    "from azure.storage.blob import generate_container_sas, ContainerSasPermissions\n",
    "\n",
    "from data_management.megadb.megadb_utils import MegadbUtils"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Update the SAS keys in the `datasets` table of MegaDB\n",
    "\n",
    "Credentials for the MegaDB, `COSMOS_ENDPOINT` and `COSMOS_KEY`, need to be environment variables. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Initialize Cosmos DB client\n",
    "url = os.environ['COSMOS_ENDPOINT']\n",
    "key = os.environ['COSMOS_WRITE_KEY']\n",
    "client = CosmosClient(url, credential=key)\n",
    "\n",
    "database = client.get_database_client('camera-trap')\n",
    "container_datasets = database.get_container_client('datasets')\n",
    "\n",
    "# Get the datasets table\n",
    "megadb_utils = MegadbUtils(url=url, key=key)\n",
    "datasets = megadb_utils.get_datasets_table()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# here are the list of storage accounts the datasets live under - should only be a handful\n",
    "\n",
    "all_sa = set()\n",
    "for dataset_name, d in datasets.items():\n",
    "    all_sa.add(d['storage_account'])\n",
    "\n",
    "all_sa"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# fill out this dict with storage account name : key\n",
    "\n",
    "sa_keys = {\n",
    "    'lilablobssc': ''\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# set an expiry date for the SAS signatures\n",
    "expiry_date = datetime(2026, 1, 1)\n",
    "\n",
    "new_datasets = []\n",
    "\n",
    "for dataset_name, d in datasets.items():\n",
    "    d_new = copy.deepcopy(d)\n",
    "    \n",
    "    key_no_query = generate_container_sas(\n",
    "        d['storage_account'], d['container'], \n",
    "        account_key=sa_keys[d['storage_account']],\n",
    "        permission=ContainerSasPermissions(read=True, list=True),\n",
    "        expiry=expiry_date\n",
    "    )\n",
    "    d_new['container_sas_key'] = '?' + key_no_query\n",
    "    new_datasets.append(d_new)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# update the entries (the \"id\" field will be used to match the new entries to existing ones and update them)\n",
    "\n",
    "for d in new_datasets:\n",
    "    res = container_datasets.upsert_item(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:cameratraps] *",
   "language": "python",
   "name": "conda-env-cameratraps-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
